﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using RED_NET_Core.BLL;
using RED_NET_Core.Entity;
using RED_NET_Core.Utilty;
using System.Drawing;
namespace RED_NET.ContentPage
{
    public partial class SendLetter : System.Web.UI.Page
    {
        public static int[] SendTo = new int[30];
        public static int num = 0;
        public static string text=null;
        private static Users user;
        public static int visitId = -1;
        protected void InitDropDownList()
        {
            LoginBLL _loginBll = new LoginBLL();
            DataTable _dt=new DataTable();
            try
            {
                _dt = _loginBll.GetMyAttentionInfo(user.UserId);
            }
            catch (Exception ex)
            {
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }
            DropDownList_MyAttention.Items.Clear();
            int i;
            DropDownList_MyAttention.Items.Add(new ListItem("", ""));
            for (i = 0; i < _dt.Rows.Count; i++)
            {
                string _itemName = _dt.Rows[i]["_NickName"].ToString();
                string _itemValue = _dt.Rows[i]["_UId"].ToString();
                DropDownList_MyAttention.Items.Add(new ListItem(_itemName, _itemValue));
            }
        }
        /// <summary>
        /// 页面初始化
        /// </summary>
        protected void Init()
        {
            Session["PageName"] = "SendLetter";
            user= (Users)Session["USER"] ;
            if (Request.QueryString["VISIT_ID"] != null)
                visitId = Int32.Parse(Request.QueryString["VISIT_ID"]);
            else
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            if (visitId != user.UserId)
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            LinkButton_Sendletter.BackColor = Color.Gray;
            LinkButton_Inbox.PostBackUrl = "~/ContentPage/News/InBox.aspx?VISIT_ID=" + visitId.ToString();
            LinkButton_Sendletter.PostBackUrl = "~/ContentPage/News/SendLetter.aspx?VISIT_ID=" + visitId.ToString();
            LinkButton_Sendbox.PostBackUrl = "~/ContentPage/News/SendBox.aspx?VISIT_ID=" + visitId.ToString();
           
            InitDropDownList();
        
            TextBox_Content.Text = null;
            TextBox_Recv.Text = null;
            TextBox_Title.Text = null;
            if (Session["SendTo_Id"] != null)
            {
                SendTo[num] = (int)Session["SendTo_Id"];
                text += (string)Session["SendTo_NickName"] + ";";
                TextBox_Recv.Text = text;
                num = 1;
                Session.Remove("SendTo_Id");
                Session.Remove("SendTo_NickName");
            }
            else
            {
                text = null;
                num = 0;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!IsPostBack)
            {
                //检测用户是否登录
                if (Session["USER"] == null)
                {
                    Response.Redirect("~/ContentPage/Login_2.aspx");


                }
                Init();
            }
              
        }

        protected void LinkButton_Send_Click(object sender, EventArgs e)
        {
            string _content=TextBox_Content.Text;
            string _title=TextBox_Title.Text;
           ReturnValue  resTitle = Safety.JudgeStr(_title, 1);
           ReturnValue resContent = Safety.JudgeStr(_content, 3);


           if (num == 0)
           {
               Label_Recv_Error.Visible = true;
           }
           else if (resTitle.value == 0&&resContent.value==0)
           {
               try
               {
                   News_BLL.AddLetter(user.UserId, _content, _title, SendTo, num, TextBox_Recv.Text.ToString());
               }
               catch (Exception ex)
               {
                   Response.Redirect("~/ContentPage/Error/PageError.aspx");
               }
               Label_SendSuccess.Visible = true;
               Init();
               num = 0;
           }
           else
           {
               Label_Title_Error.Text = resTitle.error;
               Label_Content_Error.Text = resContent.error;
           }
        }

        protected void DropDownList_MyAttention_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList_MyAttention.SelectedValue == "")
            {
              return;
             
            }
           SendTo[num]=Int32.Parse(DropDownList_MyAttention.SelectedValue);
           
           int i;
           for (i = 0; i < num; i++)
           {
               if (SendTo[i] == SendTo[num])
                   break;
           }
           if (i == num)
           {
               text += DropDownList_MyAttention.SelectedItem.Text + ";";
               TextBox_Recv.Text = text;
               num++;
           }
        }

    }
}
